<?php

namespace app\api\controller\hezi;

use app\admin\model\JingpinUser;
use app\admin\model\Xcx;
use app\common\controller\Api;
use Pheanstalk\Pheanstalk;
use think\Log;
use think\Request;

class Jingpin extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    // 模板消息数据
    public $tmp = array(
        'template_id' => '6oURlR2E010Y2AlPZo8NujJ2KLDyG0WKqelknTczJj8',
        'page' => 'pages/index/index',
        'data' => array(
            'keyword1' => '最新游戏更新',
            'keyword2' => '每天快乐源泉,尽在精品盒子',
            'keyword3' => '点击进入查看',
        )
    );

    // 登录
    public function login(Request $request)
    {
        $gh_id = $request->post('gh_id');
        $code = $request->post('code');

        $xcx = Xcx::get(['ghid' => $gh_id]);
        $url = 'https://api.weixin.qq.com/sns/jscode2session';
        $params['appid'] = $xcx->appid;
        $params['secret'] = $xcx->secret;
        $params['js_code'] = $code;
        $params['grant_type'] = 'authorization_code';

        $res = $this->http($url, $params);
        $json = json_decode($res, true);
        $openid = $json['openid'];

        $user = JingpinUser::get(['openid' => $openid]);
        if (!$user) {
            $u = new JingpinUser;
            $u->openid = $openid;
            $u->save();
            $data['is_formid'] = true;
            $data['gold'] = 0;
        } else {
            $begin_today=mktime(0,0,0,date('m'),date('d'),date('Y'));
            $begin_yesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
            if ($user->prev_formid < $begin_today) {
                $user->is_formid = 1;
            }
            // 判断上次签到时间是否小于前天,是否到达签到上限,小于则清空签到天数
            if ($user->prev_signin < $begin_yesterday || $user->signin_num >= 7) {
                $user->signin_num = 0;
            }
            $user->save();
            $data['is_formid'] = $user->is_formid ? true : false;
            $data['gold'] = $user->gold;
        }
        $data['openid'] = $openid;

        $this->result('login', $data);
    }

    // 请求数据(老版本)
    public function get_data()
    {
        $banners = array(
            array(
                'name' => '嗨爆小片',
                'appid' => 'wx38e4080448c98296',
                'banner' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/banner/hbxp1.jpg'
            ),
            array(
                'name' => '蹦蹦Jump',
                'appid' => 'wxfc2e85ad6e4c1691',
                'banner' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/banner/bbjump2.jpg'
            ),
            array(
                'name' => '天天去钓鱼',
                'appid' => 'wxbdd0d9a5187bc731',
                'banner' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/banner/fish.jpg'
            ),
        );
        $hots = array(
            array(
                'name' => '天天去钓鱼',
                'appid' => 'wxbdd0d9a5187bc731',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/fish.jpg'
            ),
            array(
                'name' => '重名查询',
                'appid' => 'wx3093c5ec75efcd7b',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/cmcx.jpg'
            ),
            array(
                'name' => '蹦蹦Jump',
                'appid' => 'wxfc2e85ad6e4c1691',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/bbjump.jpg'
            ),
            array(
                'name' => '嗨爆小片',
                'appid' => 'wx38e4080448c98296',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/hbxp.jpg'
            ),
        );
        $new = array(
            array(
                'name' => '随你搞',
                'appid' => 'wx07cac980fde4202a',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/sng.jpg'
            ),
            array(
                'name' => '魔盘',
                'appid' => 'wx1ce4c0d710fcec54',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/mp.jpg'
            ),
            array(
                'name' => '天天广场舞教学',
                'appid' => 'wx77a9968ac2c2d9e7',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/ttgcwjx.jpg'
            ),
            array(
                'name' => '悦图籍',
                'appid' => 'wxa626926803a7a03b',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/ytj.jpg'
            ),
        );
        $list = array(
            array(
                'name' => '嗨爆小片',
                'appid' => 'wx38e4080448c98296',
                'desc' => '各种好玩好看的视频聚集,一起来嗨爆',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/hbxp.jpg'
            ),
            array(
                'name' => '魔力画板',
                'appid' => 'wx72aba8c49ad40eaa',
                'desc' => '墨笔丹青,如行云流水绕素笺,展瀚海崇山依旧颜',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/mlhb.gif'
            ),
            array(
                'name' => '脑力答题',
                'appid' => 'wx8325192a2cf3611c',
                'desc' => '丰富知识,脑力答题为您带来古今中外的题目',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/nldt.jpg'
            ),
            array(
                'name' => '天天广场舞教学',
                'appid' => 'wx77a9968ac2c2d9e7',
                'desc' => '每天分享最新的广场舞,美好生活旋律从这里开始!',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/ssjqgcw.gif'
            )
        );
        $data['banners'] = $banners;
        $data['hots'] = $hots;
        $data['new'] = $new;
        $data['list'] = $list;
        $this->result('data', $data);
    }

    // 请求数据(新版本)
    public function get_list($v)
    {
        $banners = array(
            array(
                'name' => '嗨爆小片',
                'appid' => 'wx38e4080448c98296',
                'banner' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/banner/hbxp1.jpg'
            ),
            array(
                'name' => '蹦蹦Jump',
                'appid' => 'wxfc2e85ad6e4c1691',
                'banner' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/banner/bbjump2.jpg'
            ),
            array(
                'name' => '天天去钓鱼',
                'appid' => 'wxbdd0d9a5187bc731',
                'banner' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/banner/fish.jpg'
            ),
        );
        $hots = array(
            array(
                'name' => '天天去钓鱼',
                'appid' => 'wxbdd0d9a5187bc731',
                'use_num' => '39853',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/img/fish/index.gif'
            ),
            array(
                'name' => '重名查询',
                'appid' => 'wx3093c5ec75efcd7b',
                'use_num' => '37707',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/img/cmcx/index.gif'
            ),
            array(
                'name' => '蹦蹦Jump',
                'appid' => 'wxfc2e85ad6e4c1691',
                'use_num' => '30178',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/img/jump/index.gif'
            ),
            array(
                'name' => '魔盘',
                'appid' => 'wx1ce4c0d710fcec54',
                'use_num' => '23409',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/img/mopan/index.gif'
            ),
            array(
                'name' => '随你搞',
                'appid' => 'wx07cac980fde4202a',
                'use_num' => '36709',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/img/sng/index.gif'
            ),
        );
        $new = array(
            array(
                'name' => '嗨爆小片',
                'appid' => 'wx38e4080448c98296',
                'use_num' => '139853',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/img/haibao/index.gif'
            ),
            array(
                'name' => '天天广场舞教学',
                'appid' => 'wx77a9968ac2c2d9e7',
                'use_num' => '39853',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/img/ttgcwjx/index.gif'
            ),
            array(
                'name' => '悦图籍',
                'appid' => 'wxa626926803a7a03b',
                'use_num' => '21024',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/img/ytj/index.gif'
            ),
            array(
                'name' => '魔力画板',
                'appid' => 'wx72aba8c49ad40eaa',
                'use_num' => '12054',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/img/mlhb/index.gif'
            ),
            array(
                'name' => '脑力答题',
                'appid' => 'wx8325192a2cf3611c',
                'use_num' => '24784',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/img/nldt/index.gif'
            ),
        );
        $list = array(
            array(
                'name' => '随你搞',
                'appid' => 'wx07cac980fde4202a',
                'desc' => '随时随地恶搞生活',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/sng.jpg'
            ),
            array(
                'name' => '魔力画板',
                'appid' => 'wx72aba8c49ad40eaa',
                'desc' => '墨笔丹青,如行云流水绕素笺,展瀚海崇山依旧颜',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/mlhb.gif'
            ),
            array(
                'name' => '脑力答题',
                'appid' => 'wx8325192a2cf3611c',
                'desc' => '丰富知识,脑力答题为您带来古今中外的题目',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/nldt.jpg'
            ),
            array(
                'name' => '魔盘',
                'appid' => 'wx1ce4c0d710fcec54',
                'desc' => '发挥自己的想象,创建自己的艺术',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/mp.jpg'
            ),
        );
        $data['banners'] = $banners;
        $data['hots'] = $hots;
        $data['new'] = $new;
        $data['list'] = $list;
        $this->result('data', $data);
    }

    // 处理收到formid,并创建发送任务
    public function handler_formid(Request $request)
    {
        $ghid = $request->post('ghid');
        $openid = $request->post('openid');
        $formid = $request->post('formid');

        if ($formid) {
            $user = JingpinUser::get(['openid' => $openid]);
            $user->is_formid = 0;
            $user->prev_formid = time();
            $user->save();
        }

        $xcx = Xcx::get(['ghid' => $ghid]);

        $data['appid'] = $xcx->appid;
        $data['secret'] = $xcx->secret;
        $data['openid'] = $openid;
        $data['formid'] = $formid;
        $data['tmp'] = $this->tmp;

        $ser = new Pheanstalk('127.0.0.1');
        if ($ser->useTube('xcx_tmp')->put(json_encode($data), 2, 86000, 60)) {
            $this->success('ok');
        } else {
            $this->error('error');
        }
    }

    // 管道状态
    public function statsTube()
    {
        $ser = new Pheanstalk('127.0.0.1');
        $res = $ser->statsTube('xcx_tmp');
        return json($res) ? json($res) : '';
    }

}
